<?php

// +----------------------------------------------------------------------
// | MyCentOS
// +----------------------------------------------------------------------
// | Copyright (c) 2014 http://www.mycentos.com, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 琥珀 <317559272@qqq.com><http://www.mycentos.com>
// +----------------------------------------------------------------------
// | @date  : 2015-03-05 14:13:54
// +----------------------------------------------------------------------
// | @Last Modified by:  琥珀
// +----------------------------------------------------------------------
// | @Last Modified time: 2015-03-19 11:27:04
// +-----------------------------------------------------------------------



/**
 * 预约控制器
 * @authors 琥珀 (317559272@qq.com)
 */
namespace Admin\Controller;
use Think\Controller;

Class OrderController extends AdminController
{
    public function index() {

        $model = M('Order');

        //模糊搜索
        $searchname = I('searchname');

        if ($searchname) {
            if (is_numeric($searchname)) {
                $map['id|name|age|tel'] = array(intval($searchname), array('like', '%' . $searchname . '%'), intval($searchname), array('like', '%' . $searchname . '%'), '_multi' => true);
            } else {
                $map['name|disease|disease2|area|tel|zhuanjia_num|content|jiedai|media_from|engine|memo|doctor|doctor_zhudao|xiangmu|huifang|liaotian|rechecktime|author'] = array('like', '%' . (string)$searchname . '%', '%' . (string)$searchname . '%', '%' . (string)$searchname . '%', '%' . (string)$searchname . '%', '%' . (string)$searchname . '%', '%' . (string)$searchname . '%', '%' . (string)$searchname . '%', '%' . (string)$searchname . '%', '%' . (string)$searchname . '%', '%' . (string)$searchname . '%', '%' . (string)$searchname . '%', '%' . (string)$searchname . '%', '%' . (string)$searchname . '%', '%' . (string)$searchname . '%', '%' . (string)$searchname . '%', '%' . (string)$searchname . '%', '%' . (string)$searchname . '%', '%' . (string)$searchname . '%');
            }
        }
        /***********************************************************/
        /*数据读取规则:
        /*超管部门组 :超管可以获取任何数据
        /*医院部门组 :获取医院所有科室数据
        /*科室部门组 :获取医院科室数据
        /*统计部门组 :查看统计数据
        /*客服部门组 :获取个人数据及客服部门数据
        /*电话部门组 :获取个人数据及电话部门数据
        /*导医部门组 :获取个人数据及所有数据
        /***********************************************************/




        //精确搜索
        $name = I('name');
        if ($name) {
            $map['name'] = $name;
        }

        // 时间界限定义:
        $time_today_begin = mktime(0, 0, 0);

        //今天开始
        $time_today_end = $time_today_begin + 24 * 3600;

        //今天结束
        $time_yesterday_begin = $time_today_begin - 24 * 3600;

        //昨天开始
        $time_this_month_begin = mktime(0, 0, 0, date("m"), 1);

        //本月开始
        $time_this_month_end = strtotime("+1 month", $time_this_month_begin);

        //本月结束
        $time_last_month_begin = strtotime("-1 month", $time_this_month_begin);

        //上个月开始
        $time_last_day_begin = strtotime(date('Y-m-d', strtotime('last month')));

        //上个月同天开始
        $time_last_day_end = $time_last_day_begin + 24 * 3600;

        //上个月同天结束
        $time_last_month_end = strtotime("-1 month", time());

        //同期时间结束
        //条件查询
        $time = I('time');
        if ($time == "today") {
            $map['order_date'] = array(array('egt', $time_today_begin), array('lt', $time_today_end));
        } elseif ($time == "yesterday") {
            $map['order_date'] = array(array('egt', $time_yesterday_begin), array('lt', $time_today_begin));
        } elseif ($time == "lastmonthtoday") {
            $map['order_date'] = array(array('egt', $time_last_month_begin), array('lt', $time_last_month_end));
        } elseif ($time == "thismonth") {
            $map['order_date'] = array(array('egt', $time_this_month_begin), array('lt', $time_this_month_end));
        } elseif ($time == "lastmonth") {
            $map['order_date'] = array(array('egt', $time_last_month_begin), array('lt', $time_this_month_begin));
        }
        $list = $this->lists($model, $map);
        $this->assign('searchname', $searchname);
        $this->assign('name', $name);
        $this->assign('list', $list);
        $this->display();
    }
    public function view() {
        $id = I('id');
        $view = M('Order');
        $map['id'] = $id;
        $rs = $view->where($map)->find();

        $rs["liaotian"] = preg_replace("/(\s*?\r?\n\s*?)+/", "\n", text_show($rs["liaotian"]));
        $rs["liaotian"] = str_replace('<br><br>', '<br>', $rs["liaotian"]);
        $this->assign('data', $rs);
        $this->display();
    }
    public function add() {
        if (IS_POST) {
            $order = M('Order');
            $data = I('post.');
            $data['author'] = get_user_name();
            $data['addtime'] = time();
            $data['status'] = 0;
            $data['order_date'] = strtotime($data['order_date']);
            if (false === $order->create($data)) {
                $this->error($order->getError());
            }
            $list = $order->add();
            if ($list !== false) {

                $this->success('新增成功!');
            } else {
                $this->error('新增失败!');
            }
        } else {

            //获取日期
            $time = date("Y-m-d h:i:s");
            $show_days = C('show_days');

            //获取媒体来源
            $media = M("Media")->where("status = 1")->select();
            $this->assign('nowtime', $time);
            $this->assign('show_days', $show_days);
            $this->assign('media', $media);

            //分配地区
            $province = M('region')->where('type = 1')->field('id,name')->select();
            $this->assign('province', $province);

            //获取科室
            $department = M('Department')->where()->field('id,name')->select();
            $this->assign('department', $department);
            $this->display();
        }
    }
    public function edit() {
        if (IS_POST) {
            // code...

        } else {
            $id = I('id');
            if (empty($id)) {
                $this->error('请选择要操作的数据!');
            }
            $map['id'] = $id;
            $Order = M('Order');
            $data = $Order->where($map)->find();
            $hospitals = M('Hospital')->select();
            $this->assign('hospitals', $hospitals);
            $this->assign('data', $data);
            $this->display();
        }
    }
    public function del($ids = null) {
        empty($ids) && $this->error('参数错误');
        $ids = is_array($ids) ? implode(',', $ids) : $ids;
        $map['id'] = array('in', $ids);
        $res = M('Order')->where($map)->delete();
        if ($res !== false) {
            $this->success('删除成功！');
        } else {
            $this->error('删除失败！');
        }
    }
    public function get_city() {
        $map['pid'] = $_REQUEST['pid'];
        $city = M('region')->where($map)->field('id,name')->select();
        $ls = array();
        foreach ($city as $key => $value) {
            foreach ($value as $k => $v) {
                $ls[$key][] = $v;
            }
        }
        $all = array(array('', '所有城市'));
        $ls = array_merge($all, $ls);
        $json = json_encode($ls, JSON_UNESCAPED_UNICODE);
        echo $json;
    }
    public function get_area() {
        $map['pid'] = $_REQUEST['pid'];
        $area = M('region')->where($map)->field('id,name')->select();

        $ls = array();
        foreach ($area as $key => $value) {
            foreach ($value as $k => $v) {
                $ls[$key][] = $v;
            }
        }
        $json = json_encode($ls, JSON_UNESCAPED_UNICODE);
        echo $json;
    }
    public function get_disease() {
        $map['department_id'] = $_REQUEST['department_id'];
        $area = M('Disease')->where($map)->field('id,name')->select();

        $ls = array();
        foreach ($area as $key => $value) {
            foreach ($value as $k => $v) {
                $ls[$key][] = $v;
            }
        }
        $json = json_encode($ls, JSON_UNESCAPED_UNICODE);
        echo $json;
    }
}
?>